<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\member\home;

use app\index\controller\User;
use app\index\model\Type as TypeModel;
use app\warehouse\model\WareHouse as WareHouseModel;
use app\goods\model\GoodsClass as GoodsClassModel;
use app\warehouse\model\Stock as StockModel;
use app\goods\model\Size as SizeModel;
use app\goods\model\Color as ColorModel;
use think\Db;
/**             
 * 商品中心控制器
 * @package app\admin\controller
 */
class Product extends User
{
    protected function _initialize(){
        parent::_initialize();

    }
    public function index(){
    	return $this->fetch('index');
    }

    /**
    *我的收藏
    */
    public function collection(){
    	$uid = session('uid');
        /*dump(input('get.'));*/
        //款名
        $classname = input('classname');
        if(!empty($classname)){
            $where['b.classname'] = array('like',"%$classname%");
        }

        //分类
        $category = input('category');
        if(!empty($category)){
            $where['b.parentid'] = $category;
        }
        $typeid = input('typeid');
        if(!empty($typeid)){
            $where['b.typeid'] = $typeid;
        }

        //店铺
        $shop = input('shop');
        if(!empty($shop)){
            $where['a.shopid'] = $shop;
        }
        $where['memberid'] = $uid;
        $where['is_del']   = 0;
        $where['b.status'] = 1;
    	$data_list = db('member_collection a')
                    ->join('goods_class b', 'a.classid=b.classid', 'left')
                    ->field('a.*,b.classname,b.customcode,b.typeid')
                    ->where($where)
                    ->order('collectionid DESC')
                    ->paginate(10,false,['query' => request()->param(false)]);
        $all_page = $data_list->lastPage();
        $this->assign('all_page',$all_page);
        $page = $data_list->render('上一页','下一页');
        $this->assign('page',$page);
    	//转换
    	$data_list = $data_list->all();
    	foreach ($data_list as $key => $value) {
    		//sku图片信息
    		$front = db('goods_list')->where(['classid' => $value['classid'],'colorid' => $value['colorid'], 'ownerid' => $value['shopid']])->value('front');
    		$data_list[$key]['front']     = $this->getPath($front);
    		//分类信息
    		$data_list[$key]['typename']  = db('goods_type')->where('typeid', $value['typeid'])->value('classname');

    		//店铺信息
    		$data_list[$key]['shopname'] = db('shop')->where('id',$value['shopid'])->value('name');

    	}
    	$this->assign('data_list', $data_list);

        //分类
        $collection_category = TypeModel::getAll(['status' => '1', 'show' => '1', 'parentid' => '0'], 'sort ASC');

        foreach ($collection_category as $key => $value) {
            $son_category = TypeModel::getAll(['status' => '1', 'show' => '1', 'parentid' => $value['typeid']], 'sort ASC');
            $collection_category[$key]['son_category'] = $son_category;
        }
        $this->assign('collection_category', $collection_category);

        //店铺列表
        $shop_list = \think\Loader::model('app\shop\model\Shop')->getAllShop(['is_enable' => '1','status' => '1']);
        $this->assign('shop_list',$shop_list);

    	return $this->fetch('collection');
    }


    /**
    *删除收藏
    */
    public function del_collection($id = 0){
        $id  = input('id');
        $uid = session('uid');
        $result = db('member_collection')->where(['collectionid' => $id, 'memberid' => $uid])->update(['is_del' => 1]);
        if($result){
            $this->success('取消收藏成功');
        }else{
            $this->error('取消收藏失败');
        }
    }

    /**
    *删除多条收藏
    */
    public function del_allcollection($items = ''){
        $items  = input('items');
        if(empty($items)){
            $this->error('请选择取消收藏的商品');
            exit;
        }
        $items  = explode(',', $items);
        $uid    = session('info.memberid');
        $result = '';
        foreach ($items as $key => $value) {
            $result = db('member_collection')->where(['collectionid' => $value, 'memberid' => $uid])->update(['is_del' => 1]);
        }
        if($result){
            $this->success('取消收藏成功');
        }else{
            $this->error('取消收藏失败');
        }
    }

    /**
    *库存查询
    */
    public function stockquery(){
        $where=[];
        if(!empty($_GET['shopid'])) $where['storeid']=$_GET['shopid'];
        if(!empty($_GET['warehouseid'])) $where['warehouseid']=$_GET['warehouseid'];
        $goodsclass=$colordata=$sizedata=[];
        if(!empty($_GET['artno'])) {
            $goodsclass=GoodsClassModel::where('classid',$_GET['artno'])->field('artno,sizebox,colorbox')->find();
            $where['artno']=$goodsclass['artno'];
        
            $sizedata=SizeModel::where(['status'=>1,'sizeid'=>['in',$goodsclass['sizebox']]])->order('sort asc,sizeid asc')->select();
            $colordata=ColorModel::where(['status'=>1,'colorid'=>['in',$goodsclass['colorbox']]])->order('sort asc,colorid asc')->select();
            if(!empty($where)){
                foreach ($colordata as $key => $value) {
                    # code...
                    $stockdata=[];
                    foreach ($sizedata as $k => $val) {
                        $product_info=StockModel::where(['sizename'=>$val['name'],'colorname'=>$value['name']])->where($where)->field('waitnum,skuid')->find();
                        $stockdata[$k]['num']=!empty($product_info['waitnum'])?$product_info['waitnum']:'-';
                        $stockdata[$k]['skuid']=!empty($product_info['skuid'])?$product_info['skuid']:'';
                    }
                    $colordata[$key]['stockdata']=$stockdata;
                }
            }else{
                $colordata=[];
            }
        }
        $this->assign("colordata",$colordata);
        $this->assign("sizedata",$sizedata);
        $class_list = GoodsClassModel::where('status','1')->field('classid,artno')->select();
        $this->assign("typedata",$class_list);

        $warehousedata=WareHouseModel::where(['status'=>1,'isdel'=>0])->field('warehouseid,warehousename')->select();
        $this->assign("warehousedata",$warehousedata);

        $shopdata = \think\Loader::model('app\shop\model\Shop')->getAllShop(['is_enable' => '1','status' => '1']);
        $this->assign('shopdata',$shopdata);
        return $this->fetch('stockquery');
    }
}